package controller;

import domain.Bill;
import domain.Provider;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.BillService;
import services.ProviderService;
import utils.PageBean;
import utils.Transform;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bill")
public class BillController {
    @Resource
    private BillService billService;
    @Resource
    private ProviderService providerService;

    /**
     * 根据条件分页查询所有订单
     * @param request
     * @return
     */
    @RequestMapping("/queryAllBill.action")
    public String queryAllBill(HttpServletRequest request){
        PageBean pageBean = new PageBean();
        List<Bill> billList = null;
        int pid = 0;
        int isPayment = 0;
        int totalCount = billService.totalPage();
        //获取请求参数
        String queryProductName = request.getParameter("queryProductName");
        String queryProviderId = request.getParameter("queryProviderId");
        String queryIsPayment = request.getParameter("queryIsPayment");
        String pageIndex = request.getParameter("pageIndex");
        if(pageIndex == null){
            pageIndex = "1";
        }
        if(queryIsPayment!=null&&queryProviderId!=null){
            pid = Integer.parseInt(queryProviderId);
            isPayment = Integer.parseInt(queryIsPayment);
        }
        //初始化pageBean
        pageBean.setCurrentPage(Integer.parseInt(pageIndex));
        pageBean.setTotal(totalCount);

        //调用业务层接口
        List<Provider> providerList = providerService.queryAll();
        billList = billService.queryBillByPage(queryProductName,
                pid, isPayment,pageBean);
        //共享数据
        request.setAttribute("billList",billList);
        request.setAttribute("providerList",providerList);
        request.setAttribute("totalPageCount",pageBean.getTotalPage());
        request.setAttribute("totalCount",totalCount);
        request.setAttribute("currentPageNo",pageIndex);
        request.setAttribute("queryProductName",queryProductName);
        request.setAttribute("queryProviderId",queryProviderId);
        request.setAttribute("queryIsPayment",queryIsPayment);

        return "forward:../jsp/billlist.jsp";
    }

    /**
     * 添加订单
     * @param request
     * @return
     */
    @RequestMapping("/addBill.action")
    public String addBill(HttpServletRequest request){
        Map<String,String[]> params = request.getParameterMap();
        boolean flag = false;
        Bill bill = new Bill();
        //注册BeanUtils格式转化器
        Transform.beanUtilsConvert();
        //封装Bill
        try {
            BeanUtils.populate(bill,params);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        flag = billService.addBill(bill);
        if(flag){
            request.setAttribute("message","添加成功");
        }else{
            request.setAttribute("message","添加失败");
        }
        return "forward:../jsp/billadd.jsp";
    }

    /**
     * 根据指定id查看订单
     * @param request
     * @return
     */
    @RequestMapping("/viewBill.action")
    public String viewBill(HttpServletRequest request){
        Bill bill = null;
        String path = "";
        String id = request.getParameter("billid");
        String method = request.getParameter("method");

        if(id!=null){
            bill = billService.queryBillById(Integer.parseInt(id));
        }

        if(method.equals("view")){
            path = "forward:../jsp/billview.jsp";
        }else if(method.equals("modify")){
            path = "forward:../jsp/billmodify.jsp";
        }
        request.setAttribute("bill",bill);
        return path;
    }

    /**
     * 修改订单
     * @param request
     * @return
     */
    @RequestMapping("/modifyBill.action")
    public String modifyBill(HttpServletRequest request){
        Bill bill  = new Bill();
        boolean flag = false;
        //获取请求参数
        Map<String,String[]> params = request.getParameterMap();
        //注册BeanUtils格式转化器
        Transform.beanUtilsConvert();
        //封装Bill
        try {
            BeanUtils.populate(bill,params);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用业务接口
        flag = billService.modifyBill(bill);
        if(flag){
            request.setAttribute("message","修改成功");
        }else{
            request.setAttribute("message","修改失败");
        }
        request.setAttribute("bill",bill);
        return "forward:../jsp/billmodify.jsp";
    }

    /**
     * ajax删除订单
     * @param request
     * @return
     */
    @RequestMapping("/deleteBill.action")
    @ResponseBody
    public Object deleteBill(HttpServletRequest request){
        String bid = request.getParameter("billid");
        int flag = 0;
        HashMap<String,String> res = new HashMap<>();
        if(bid == null){
            bid = "0";
        }
        flag = billService.deleteBill(Integer.parseInt(bid));
        if(flag == 0){
            res.put("delResult","false");
        }else if(flag == 1){
            res.put("delResult","notexist");
        }else if(flag == 2){
            res.put("delResult","true");
        }
        return res;
    }
}
